// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;

namespace SnapObjects.Data
{
    internal abstract class SqlHavingAndOr<TCondition> : SqlBuilderComb, ISqlHavingAndOr<TCondition>
    {
        protected SqlBuildClause _clause;
        public SqlHavingAndOr(ISqlBuilder builder, SqlBuildClause having) : base(builder)
        {
            _clause = having;
        }

        protected abstract TCondition BuilderResult();

        private TCondition HavingCondition(ConditionElement condition, SqlStitchingType stitchingType)
        {
            if (_clause.TryGet(out HavingElement element))
            {
                _clause.Changed();

                element.Condition.AndOr.Add(condition, stitchingType);
            }
            else
            {
                throw new InvalidOperationException("Please call 'Having' before adding conditions.");
            }

            return this.BuilderResult();
        }

        /// <summary>
        ///     Adds the AND logical operator and a search condition to the HAVING clause. Specifies a SQL expression to the left of the 
        ///     operator and a SQL parameter to the right of the operator. The operator is '='.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="parameter">
        ///     An ISqlParameter object which represents a SQL parameter on the right of the operator. 
        ///     It can be created by calling the SqlBuilder.Parameter method.
        /// </param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public TCondition AndHaving(string left, ISqlParameter parameter)
        {
            return this.AndHaving(left, SqlBinaryOperator.Equals, parameter);
        }

        /// <summary>
        ///     Adds the AND logical operator and a search condition to the HAVING clause. Specifies a SQL expression to the left of the
        ///     operator and a SQL parameter to the right of the operator.
        /// </summary>
        /// <param name="left">A SQL expression to the left of the operator.</param>
        /// <param name="sqlOperator">
        ///     An enumeration value of SqlBinaryOperator, which is the operator to test the two expressions on the left and right.
        /// </param>
        /// <param name="parameter">
        ///     An ISqlParameter object which represents a SQL parameter to the right of the operator. 
        ///     It can be created by calling the SqlBuilder.Parameter method.
        /// </param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public TCondition AndHaving(string left, SqlBinaryOperator sqlOperator, ISqlParameter parameter)
        {
            _clause.AddParameter(parameter);

            return this.HavingCondition(
                new ConditionElement(
                    left, SqlOperatorConverter.ToSqlOperator(sqlOperator), parameter, SqlRightType.Parameter),
                SqlStitchingType.And);
        }

        /// <summary>
        ///     Adds the AND logical operator and a search condition to the HAVING clause. Specifies SQL expressions on both the left
        ///     and right of the operator. The operator is '='.
        /// </summary>
        /// <param name="left">A SQL expression to the left of the operator.</param>
        /// <param name="right">A SQL expression to the right of the operator.</param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public TCondition AndHaving(string left, string right)
        {
            return this.HavingCondition(
                new ConditionElement(left, SqlOperator.Equals, right, SqlRightType.Clause),
                SqlStitchingType.And);
        }

        /// <summary>
        ///     Adds the AND logical operator and a search condition to the HAVING clause. Specifies SQL expressions on both the left 
        ///     and right of the operator.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="sqlOperator">
        ///     An enumeration value of SqlBinaryOperator, which is the operator to test the two expressions on the left and right.
        /// </param>
        /// <param name="right">A SQL expression to the right of the operator.</param>
        /// <returns>
        ///      Returns the TBuilder object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public TCondition AndHaving(string left, SqlBinaryOperator sqlOperator, string right)
        {
            return this.HavingCondition(
                new ConditionElement(
                    left, SqlOperatorConverter.ToSqlOperator(sqlOperator), right, SqlRightType.Clause),
                SqlStitchingType.And);
        }

        /// <summary>
        ///     Adds the AND logical operator and a search condition to the HAVING clause. Specifies a SQL expression on the left of 
        ///     the operator and a SQL subquery on the right of the operator. The operator is '='.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="sqlBuilder">
        ///     An ISqlBuilder object which represents a SQL subquery on the right of the operator.
        /// </param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public TCondition AndHaving(string left, ISqlBuilder sqlBuilder)
        {
            return this.AndHaving(left, SqlBinaryOperator.Equals, sqlBuilder);
        }

        /// <summary>
        ///     Adds the AND logical operator and a search condition to the HAVING clause. Specifies a SQL expression on the left of 
        ///     the operator and a SQL subquery on the right of the operator.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="sqlOperator">
        ///     An enumeration value of SqlBinaryOperator, which is the operator to test the two expressions on the left and right.
        /// </param>
        /// <param name="sqlBuilder">
        ///     An ISqlBuilder object which represents a SQL subquery on the right of the operator.
        /// </param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public TCondition AndHaving(string left, SqlBinaryOperator sqlOperator, ISqlBuilder sqlBuilder)
        {
            var builder = (SqlBuilderBase)sqlBuilder;

            _clause.AddParameters(builder.Statement.Parameters);

            return this.HavingCondition(
                new ConditionElement(
                    left, SqlOperatorConverter.ToSqlOperator(sqlOperator), builder.Statement, SqlRightType.Statement),
                SqlStitchingType.And);
        }

        /// <summary>
        ///     Adds the AND logical operator and a search condition (specified by an ISqlHavingCondition object) to the HAVING clause.
        /// </summary>
        /// <param name="condition">
        ///     An ISqlHavingCondition object which represents a search condition in the HAVING clause. 
        ///     This object can be created by using the SqlBuilder.Having method and so on.
        /// </param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public TCondition AndHaving(ISqlHavingCondition condition)
        {
            return this.AndHaving(condition, "");
        }

        /// <summary>
        ///     Adds the AND logical operator and a search condition (specified by an ISqlHavingCondition object) to the HAVING clause. 
        ///     You can specify an alias for the search condition.
        /// </summary>
        /// <param name="condition">
        ///     An ISqlHavingCondition object which represents a search condition in the HAVING clause. 
        ///     This object can be created by using the SqlBuilder.Having method and so on.
        /// </param>
        /// <param name="alias">An alias for the search condition.</param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public TCondition AndHaving(ISqlHavingCondition condition, string havingName)
        {
            var hc = (SqlHavingCondition)condition;

            if (hc.Clause.TryGet(out HavingElement chaving))
            {
                if (_clause.TryGet(out HavingElement element))
                {
                    _clause.Changed();

                    chaving.Condition.ElementName = havingName;

                    element.Condition.AndOr.Add(chaving.Condition, SqlStitchingType.And);
                }
                else
                {
                    chaving.ElementName = havingName;

                    _clause.Add(chaving);
                }
                _clause.AddParameters(hc.BuilderParameters);
            }

            return this.BuilderResult();
        }

        /// <summary>
        ///     Adds the AND logical operator and a raw SQL to the HAVING clause.
        /// </summary>
        /// <param name="clause">A string of raw SQL to be added to the HAVING clause.</param>
        /// <param name="parameters">
        ///     (Optional) An array of ISqlParameter objects which define the SQL parameters used in the raw SQL.
        /// </param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public TCondition AndHavingRaw(string clause, params ISqlParameter[] parameters)
        {
            return this.AndHavingRaw(clause, "", parameters);
        }

        /// <summary>
        ///     Adds the AND logical operator and a raw SQL string to the HAVING clause. You need to specify an alias for the raw SQL.
        /// </summary>
        /// <param name="clause">A string of raw SQL to be added to the HAVING clause.</param>
        /// <param name="alias">The alias specified for the raw SQL.</param>
        /// <param name="parameters">
        ///     (Optional) An array of ISqlParameter objects which define the SQL parameters used in the raw SQL.
        /// </param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public TCondition AndHavingRaw(string clause, string havingName, params ISqlParameter[] parameters)
        {
            _clause.AddParameters(parameters);

            return this.HavingCondition(
                new ConditionElement(
                    null, SqlOperator.Equals, clause, SqlRightType.Clause, havingName),
                SqlStitchingType.And);
        }

        /// <summary>
        ///     Adds the AND logical operator and a search condition to the HAVING clause. Specifies a SQL expression on the left the 
        ///     operator and a specific value on the right of the operator. The operator is '='.
        /// </summary>
        /// <param name="left">An expression on the left of the operator.</param>
        /// <param name="value">The specific value on the right of the operator.</param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public TCondition AndHavingValue(string left, object value)
        {
            return this.AndHavingValue(left, SqlBinaryOperator.Equals, value);
        }

        /// <summary>
        ///     Adds the AND logical operator and a search condition to the HAVING clause. Specifies a SQL expression on the left the 
        ///     operator and a specific value on the right of the operator.
        /// </summary>
        /// <param name="left">An expression on the left of the operator.</param>
        /// <param name="sqlOperator">
        ///     An enumeration value of SqlBinaryOperator, which is the operator to test the expression on the left and the value on the right.
        /// </param>
        /// <param name="value">The specific value on the right of the operator.</param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public TCondition AndHavingValue(string left, SqlBinaryOperator sqlOperator, object value)
        {
            return this.HavingCondition(
                new ConditionElement(
                    left, SqlOperatorConverter.ToSqlOperator(sqlOperator), value, SqlRightType.Value),
                SqlStitchingType.And);
        }

        /// <summary>
        ///     Adds the OR logical operator and a search condition to the HAVING clause. Specifies a SQL expression on the 
        ///     left of the operator and a SQL parameter on the right of the operator. The operator is '='.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="parameter">
        ///     An ISqlParameter object which represents a SQL parameter on the right of the operator.
        ///     It can be created by calling the SqlBuilder.Parameter method.
        /// </param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public TCondition OrHaving(string left, ISqlParameter parameter)
        {
            return this.OrHaving(left, SqlBinaryOperator.Equals, parameter);
        }

        /// <summary>
        ///     Adds the OR logical operator and a search condition to the HAVING clause. Specifies a SQL expression to the
        ///     left of the operator and a SQL parameter to the right of the operator.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="sqlOperator">
        ///     An enumeration value of SqlBinaryOperator, which is the operator to test the two expressions on the left and right.
        /// </param>
        /// <param name="parameter">
        ///     An ISqlParameter object which represents a SQL parameter on the right of the operator.
        ///     It can be created by calling the SqlBuilder.Parameter method.
        /// </param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public TCondition OrHaving(string left, SqlBinaryOperator sqlOperator, ISqlParameter parameter)
        {
            _clause.AddParameter(parameter);

            return this.HavingCondition(
                new ConditionElement(
                    left, SqlOperatorConverter.ToSqlOperator(sqlOperator), parameter, SqlRightType.Parameter),
                SqlStitchingType.Or);
        }

        /// <summary>
        ///     Adds the OR logical operator and a search condition to the HAVING clause. Specifies SQL expressions on both the 
        ///     left and right of the operator. The operator is '='.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="right">A SQL expression on the right of the operator.</param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public TCondition OrHaving(string left, string right)
        {
            return this.OrHaving(left, SqlBinaryOperator.Equals, right);
        }

        /// <summary>
        ///     Adds the OR logical operator and a search condition to the HAVING clause. Specifies SQL expressions 
        ///     on both the left and right of the operator.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="sqlOperator">
        ///     An enumeration value of SqlBinaryOperator, which is the operator to test the two expressions on the left and right.
        /// </param>
        /// <param name="right">A SQL expression on the right of the operator.</param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public TCondition OrHaving(string left, SqlBinaryOperator sqlOperator, string right)
        {
            return this.HavingCondition(
                new ConditionElement(left, SqlOperatorConverter.ToSqlOperator(sqlOperator), right, SqlRightType.Clause),
                SqlStitchingType.Or);
        }

        /// <summary>
        ///     Adds the OR logical operator and a search condition to the HAVING clause. Specifies a SQL expression on the left of
        ///     the operator and a SQL subquery on the right of the operator. The operator is '='.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="sqlBuilder">
        ///     An ISqlBuilder object which represents a SQL subquery on the right of the operator.
        /// </param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public TCondition OrHaving(string left, ISqlBuilder sqlBuilder)
        {
            return this.OrHaving(left, SqlBinaryOperator.Equals, sqlBuilder);
        }

        /// <summary>
        ///     Adds the OR logical operator and a search condition to the HAVING clause. Specifies a SQL expression on the left of
        ///     the operator and a SQL subquery on the right of the operator.
        /// </summary>
        /// <param name="left">A SQL expression on the left of the operator.</param>
        /// <param name="sqlOperator">
        ///     An enumeration value of SqlBinaryOperator, which is the operator to test the two expressions on the left and right.
        /// </param>
        /// <param name="sqlBuilder">
        ///     An ISqlBuilder object which represents a SQL subquery on the right of the operator.
        /// </param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public TCondition OrHaving(string left, SqlBinaryOperator sqlOperator, ISqlBuilder sqlBuilder)
        {
            var builder = (SqlBuilderBase)sqlBuilder;

            _clause.AddParameters(builder.Statement.Parameters);

            return this.HavingCondition(
                new ConditionElement(
                    left, SqlOperatorConverter.ToSqlOperator(sqlOperator), builder.Statement, SqlRightType.Statement),
                SqlStitchingType.Or);
        }

        /// <summary>
        ///     Adds the OR logical operator and a search condition (specified by an ISqlHavingCondition object) to the HAVING clause.
        /// </summary>
        /// <param name="condition">
        ///     An ISqlHavingCondition object which represents a search condition of the HAVING clause.
        ///     This object can be created by using the SqlBuilder.Having method and so on.
        /// </param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public TCondition OrHaving(ISqlHavingCondition condition)
        {
            return this.OrHaving(condition, "");
        }

        /// <summary>
        ///     Adds the OR logical operator and a search condition (specified by an ISqlHavingCondition object) to the HAVING clause. 
        ///     You can specify an alias for the search condition.
        /// </summary>
        /// <param name="condition">
        ///     An ISqlHavingCondition object which represents a search condition of the HAVING clause.
        ///     This object can be created by using the SqlBuilder.Having method and so on.
        /// </param>
        /// <param name="alias">An alias for the search condition.</param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public TCondition OrHaving(ISqlHavingCondition condition, string havingName)
        {
            var hc = (SqlHavingCondition)condition;

            if (hc.Clause.TryGet(out HavingElement chaving))
            {
                if (_clause.TryGet(out HavingElement element))
                {
                    _clause.Changed();

                    chaving.Condition.ElementName = havingName;
                    element.Condition.AndOr.Add(chaving.Condition, SqlStitchingType.Or);
                }
                else
                {
                    chaving.ElementName = havingName;
                    _clause.Add(chaving);
                }

                _clause.AddParameters(hc.BuilderParameters);
            }

            return this.BuilderResult();
        }

        /// <summary>
        ///     Adds the OR logical operator and a raw SQL to the HAVING clause.
        /// </summary>
        /// <param name="clause">A string of raw SQL to be added to the HAVING clause.</param>
        /// <param name="parameters">
        ///     (Optional) An array of ISqlParameter objects which define the SQL parameters used in the raw SQL.
        /// </param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public TCondition OrHavingRaw(string clause, params ISqlParameter[] parameters)
        {
            return this.OrHavingRaw(clause, "", parameters);
        }

        /// <summary>
        ///     Adds the OR logical operator and a raw SQL to the HAVING clause. You need to specify an alias for the raw SQL.
        /// </summary>
        /// <param name="clause">A string of raw SQL to be added to the HAVING clause.</param>
        /// <param name="alias">The alias specified for the raw SQL.</param>
        /// <param name="parameters">
        ///     (Optional) An array of ISqlParameter objects which define the SQL parameters used in the raw SQL.
        /// </param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public TCondition OrHavingRaw(string clause, string havingName, params ISqlParameter[] parameters)
        {
            _clause.AddParameters(parameters);

            return this.HavingCondition(
                new ConditionElement(
                    null, SqlOperator.Equals, clause, SqlRightType.Clause, havingName),
                SqlStitchingType.Or);
        }

        /// <summary>
        ///     Adds the OR logical operator and a search condition to the HAVING clause. Specifies the SQL expression on the left 
        ///     the operator and a specific value on the right of the operator. The operator is '='.
        /// </summary>
        /// <param name="left">An expression on the left of the operator.</param>
        /// <param name="value">The specific value on the right of the operator.</param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public TCondition OrHavingValue(string left, object value)
        {
            return this.OrHavingValue(left, SqlBinaryOperator.Equals, value);
        }

        /// <summary>
        ///     Adds the OR logical operator and a search condition to the HAVING clause. Specifies a SQL expression on the left 
        ///     the operator and a specific value on the right of the operator.
        /// </summary>
        /// <param name="left">An expression on the left of the operator.</param>
        /// <param name="sqlOperator">
        ///     An enumeration value of SqlBinaryOperator, which is the operator to test the expression on the left and the value on the right.
        /// </param>
        /// <param name="value">
        ///     The specific value on the right of the operator.
        /// </param>
        /// <returns>
        ///     Returns the TBuilder object which can be used to add more search conditions to the current HAVING clause.
        /// </returns>
        public TCondition OrHavingValue(string left, SqlBinaryOperator sqlOperator, object value)
        {
            return this.HavingCondition(
                new ConditionElement(left, SqlOperatorConverter.ToSqlOperator(sqlOperator), value, SqlRightType.Value),
                SqlStitchingType.Or);
        }
    }
}
